// 主题管理工具

export type ThemeType = 'tech-dark' | 'tech-blue' | 'tech-purple'

export interface ThemeColors {
  primary: string
  secondary: string
  accent: string
  bgPrimary: string
  bgSecondary: string
  bgCard: string
  textPrimary: string
  textSecondary: string
  textDim: string
}

export const themes: Record<ThemeType, ThemeColors> = {
  'tech-dark': {
    primary: '#00f3ff',
    secondary: '#ff00ff',
    accent: '#00ff88',
    bgPrimary: '#0a0e27',
    bgSecondary: '#1a1f3a',
    bgCard: 'rgba(26, 31, 58, 0.6)',
    textPrimary: '#ffffff',
    textSecondary: '#8892b0',
    textDim: '#495670'
  },
  'tech-blue': {
    primary: '#00ccff',
    secondary: '#0099ff',
    accent: '#66ccff',
    bgPrimary: '#0a1628',
    bgSecondary: '#1a2a3a',
    bgCard: 'rgba(26, 42, 58, 0.6)',
    textPrimary: '#ffffff',
    textSecondary: '#8899b0',
    textDim: '#495670'
  },
  'tech-purple': {
    primary: '#cc00ff',
    secondary: '#9900ff',
    accent: '#cc66ff',
    bgPrimary: '#1a0a28',
    bgSecondary: '#2a1a3a',
    bgCard: 'rgba(42, 26, 58, 0.6)',
    textPrimary: '#ffffff',
    textSecondary: '#9988b0',
    textDim: '#594670'
  }
}

/**
 * 应用主题到 CSS 变量
 */
export function applyTheme(theme: ThemeType): void {
  const colors = themes[theme]
  const root = document.documentElement

  root.style.setProperty('--color-primary', colors.primary)
  root.style.setProperty('--color-secondary', colors.secondary)
  root.style.setProperty('--color-accent', colors.accent)
  root.style.setProperty('--bg-primary', colors.bgPrimary)
  root.style.setProperty('--bg-secondary', colors.bgSecondary)
  root.style.setProperty('--bg-card', colors.bgCard)
  root.style.setProperty('--text-primary', colors.textPrimary)
  root.style.setProperty('--text-secondary', colors.textSecondary)
  root.style.setProperty('--text-dim', colors.textDim)
}

/**
 * 获取当前主题
 */
export function getCurrentTheme(): ThemeType {
  return (localStorage.getItem('theme') as ThemeType) || 'tech-dark'
}

/**
 * 保存主题设置
 */
export function saveTheme(theme: ThemeType): void {
  localStorage.setItem('theme', theme)
  applyTheme(theme)
}

/**
 * 初始化主题
 */
export function initTheme(): void {
  const theme = getCurrentTheme()
  applyTheme(theme)
}
